package com.lmr.boot.controller;

import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

@Controller
@Log4j2
public class FormTestController {
    @GetMapping("/form_layouts")
    public String form_layouts(){
        return "/form/form_layouts";
    }

    /**
     * MultipartFile自动封装上传上来的文件
     * @param email
     * @param username
     * @param headerImg
     * @param photos
     * @return
     */
    @PostMapping("/upload")
    public String upload(@RequestParam("email") String email, //获取邮件
                         @RequestParam("userName") String username,//获取用户名
                         @RequestParam("headerImg")MultipartFile headerImg,//获取照片
                         @RequestParam("photos") MultipartFile[] photos//获取一组照片
                         ) throws IOException {

        log.info("上传的信息:eamil={}, username={},headerImg={},photos={}",email,username,headerImg.getClass(),photos.length);
        if(!headerImg.isEmpty()){
            //保存到文件服务器，OSS服务器
            String originalFilename = headerImg.getOriginalFilename();
            headerImg.transferTo(new File("H:\\cache\\"+originalFilename));
        }

        if(photos.length > 0){
            for (MultipartFile photo : photos) {
                if(!photo.isEmpty()){
                    String originalFilename = photo.getOriginalFilename();
                    photo.transferTo(new File("H:\\cache\\"+originalFilename));
                }
            }
        }

        return "index";
    }

    @GetMapping("/abc")
    public String abc(){
        System.out.println("aaaaaaa");
        return "form/bcd";
    }
}
